import requests

from untils.log_ import logger

base_url = "https://qyapi.weixin.qq.com/cgi-bin/"


class Department():

    def __init__(self):
        """
        获取access_token
        :return:
        """
        ID = "ww1bfd9e02fe64ee02"
        SECRET = "KHXisQRKv6VTUWEbw6raKVWt9nbLLteAWfO_wdjjAhs"
        response = requests.get(base_url + f"gettoken?corpid={ID}&corpsecret={SECRET}")
        self.access_token = response.json()["access_token"]

    def add(self, data):
        url = base_url + "department/create"
        params = {"access_token": self.access_token}
        response = requests.post(url=url, json=data, params=params)
        # logger.info(response.json())
        return response.json()

    def update(self):
        url = base_url + "department/update"
        params = {"access_token": self.access_token}
        data = {
            "id": 2,
            "name": "郑州研发中心",
            "name_en": "RDGZ",
            "parentid": 1,
            "order": 1
        }
        response = requests.post(url=url, json=data, params=params)

        return response.json()

    def delete(self, id):
        url = base_url + "department/delete"
        params = {"access_token": self.access_token, "id": id}
        response = requests.post(url=url, params=params)
        return response.json()


if __name__ == '__main__':
    department = Department()
    department.update()
